package com.vgemv.jsutilitysdk.log;

import android.content.Context;

import com.vgemv.jsutilitysdk.log.target.JsLogTargetConsole;
import com.vgemv.jsutilitysdk.log.target.JsLogTargetDailyRollingFile;
import com.vgemv.jsutilitysdk.log.target.JsLogTargetView;

import java.util.ArrayList;


/**
 * 有两颜色:
 * mColor_historyLog;
 * mClolor_newLog;
 *
 * 满足以下条件之一的消息,用mColor_newMessage来显示
 * 1,最新的mint_newLogCount个日志
 * 2,消息距离当前显示时间<=mDate_logInterval之
 */

/**
 * 一个功能可能牵涉到一系列的对象.将日志按照功能分类.便于调试.
 * 提供一个功能的枚举
 *
 */

/**
 * Tag功能
 * 1,维护一个Tag列表.
 * 2,可以配置显示哪些tag的信息.
 *
 */


public class JsLogger{


    private JsLogTargetConsole mConsoleTarget = JsLogTargetConsole.sharedInstance();
    public JsLogTargetDailyRollingFile mFileTarget;
    public JsLogTargetView mViewTarget = null;
    public int logLevelFilter = JsLogManager.JsLogLevelVerbose;
    public ArrayList<String> logTagFilters = new ArrayList<String>();

    public JsLogger(String logDirPath) {
        mFileTarget = new JsLogTargetDailyRollingFile(logDirPath);
        mConsoleTarget = JsLogTargetConsole.sharedInstance();
    }

    /**
     * filetarget 默认是必须实现的.
     * @param logDirPath
     * @param context
     */
    public JsLogger(String logDirPath,Context context) {
        mFileTarget = new JsLogTargetDailyRollingFile(logDirPath);
        mConsoleTarget = JsLogTargetConsole.sharedInstance();
        mViewTarget = new JsLogTargetView(context);
    }


    /*
    1,生成一个日志事件;
    2,判断日志事件是否可以输出
    3,可以输出,则输出到对应的target上.
    4,logTarget是一个Option值.
    */
    public void log(JsLogEvent logEvent) {
        if(canLogEvent(logEvent)) {
            if((logEvent.logTarget & JsLogManager.JsLogTargetConsole) == JsLogManager.JsLogTargetConsole) {
                mConsoleTarget.log(logEvent);
            }
            if((logEvent.logTarget & JsLogManager.JsLogTargetFile) == JsLogManager.JsLogTargetFile) {
                mFileTarget.log(logEvent);
            }
            if((logEvent.logTarget & JsLogManager.JsLogTargetView) == JsLogManager.JsLogTargetView) {
                if(mViewTarget != null) {
                    mViewTarget.log(logEvent);
                }
            }
        }
    }

    public void initViewLogTarget(Context context) {
        if(mViewTarget == null) {
            mViewTarget = new JsLogTargetView(context);
        }
    }

    //判断LogTag和LogLevel是否Pass过滤.
    public boolean canLogEvent(JsLogEvent logEvent) {
        if(logEvent.logLevel < logLevelFilter) {
            return false;
        }
        if(logTagFilters.size() > 0 && !logTagFilters.contains(logEvent.context)) {
            return false;
        }
        return true;
    }
}
